package cycles.util.text;

import java.util.Random;

public class RandText {

	private static final String charbase16 = "0123456789ABCDEF";
	private static final Random rand = new Random(System.currentTimeMillis());

	private String charsrc;

	/**
	 * 源数字串
	 * 
	 * @param base
	 *            源数字的进制，应该是2,8,10，16，如2进制则源为01，16进制源为0123456789ABCDEF
	 */
	public RandText(int base) {
		if (base > 16) {
			base = 16;
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < base; i++) {
			sb.append(charbase16.charAt(i));
		}
		this.charsrc = sb.toString();
	}

	/**
	 * 指定字符
	 * 
	 * @param charsrc
	 */
	public RandText(String charsrc) {
		this.charsrc = charsrc;
	}

	/**
	 * 从chars源创建len长的随机字符串
	 * @param chars
	 * @param len
	 * @return
	 */
	public static final String create(String chars, int len) {
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			sb.append(chars.charAt(rand.nextInt(chars.length())));
		}
		return sb.toString();
	}

	/**
	 * 从字符源创建随机的len长的字符串
	 * 
	 * @param len
	 * @return
	 */
	public String create(int len) {
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			sb.append(charsrc.charAt(rand.nextInt(charsrc.length())));
		}
		return sb.toString();
	}

	/**
	 * 简单的随机数字
	 * 
	 * @param max
	 * @return
	 */
	public static final int rand(int max) {
		return rand.nextInt(max);
	}

	/**
	 * 简单的随机数字
	 * 
	 * @param min
	 * @param max
	 * @return
	 */
	public static final int rand(int min, int max) {
		return min + rand.nextInt(max - min);
	}

	public static void main(String[] args) {
		// System.out.println(new RandText("dsfsdffd").create(10));
		String a = "0dkfjsldf";
		System.out.println(a.charAt(0));
	}

}
